﻿using System;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.StaffPages.Sch
{
    /// <summary>
    /// Search 的摘要说明。
    /// </summary>
    public partial class SearchPage : Page
    {
        private readonly ListItem[] Fields = {
            new ListItem("用户名", "0"),
            new ListItem("姓名", "1"),
            new ListItem("手机", "2"),
            new ListItem("性别", "3"),
            new ListItem("Email", "4"),
            new ListItem("职位", "5"),
        };

        private readonly string[] FieldsName = { "StaffName", "RealName", "Mobile", "Gender", "Email", "PositionName" };
        private string TopTable = "";

        #region 查询选项

        //登陆名或姓名
        public string StaffName
        {
            get { return tbx_Name.Text; }
        }

        //所选职位
        public int Postion
        {
            get { return Int32.Parse(ddl_Position.SelectedValue); }
        }

        //手机号码
        public string Mobile
        {
            get { return tbx_Mobile.Text; }
        }

        //Email
        public string Email
        {
            get { return tbx_Email.Text; }
        }

        //性别
        public string Gender
        {
            get { return ddl_Gender.SelectedValue; }
        }

        //手机选项
        public bool MobileSwitch
        {
            get { return cbx_Mobile.Checked; }
        }

        //Email选项
        public bool EmailSwitch
        {
            get { return cbx_Email.Checked; }
        }

        //性别选项
        public bool GenderSwitch
        {
            get { return cbx_Gender.Checked; }
        }

        //查询选项
        public string SearchBound
        {
            get { return ddl_SearchBound.SelectedValue; }
        }

        //显示的字段,内容是datagrid的列序号
        public int[] SelectedFields
        {
            get
            {
                var number = new int[lbx_SelectedFields.Items.Count];
                for (int i = 0; i < number.Length; i++)
                {
                    number[i] = Int32.Parse(lbx_SelectedFields.Items[i].Value);
                }
                return number;
            }
        }

        //选中的字段名
        public string[] SelectedFieldsName
        {
            get
            {
                var name = new string[lbx_SelectedFields.Items.Count];
                for (int i = 0; i < name.Length; i++)
                {
                    name[i] = FieldsName[Int32.Parse(lbx_SelectedFields.Items[i].Value)];
                }
                return name;
            }
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            btn_Search.Click += btn_Search_Click;
            lbtn_Others.Click += lbtn_Others_Click;
            lbtn_SelectField.Click += lbtn_SelectField_Click;
            btn_Out.Click += btn_Out_Click;
            btn_In.Click += btn_In_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            const string ScriptFormat = "document.getElementById('{0}').disabled = !document.getElementById('{0}').disabled";

            cbx_Mobile.Attributes["onclick"] = string.Format(ScriptFormat, tbx_Mobile.ClientID);
            cbx_Email.Attributes["onclick"] = string.Format(ScriptFormat, tbx_Email.ClientID);
            cbx_Gender.Attributes["onclick"] = string.Format(ScriptFormat, ddl_Gender.ClientID);
            if (!Page.IsPostBack)
            {
                table_Other.Visible = false;
                table_Field.Visible = false;
                
                BindPosition();
                BindDefaultField();
                ViewState["TopTable"] = TopTable;
            }
            else
            {
                TopTable = ViewState["TopTable"].ToString();
            }

            base.OnLoad(e);
        }

        private void BindPosition()
        {
            var db = new Database();
            using (SqlDataReader dr = db.RunDataReader("dbo.sproc_Ext_GetPosition"))
            {
                ddl_Position.DataSource = dr;
                ddl_Position.DataTextField = "Position_Name";
                ddl_Position.DataValueField = "Position_ID";
                ddl_Position.DataBind();
            }
            ddl_Position.Items.Insert(0, new ListItem("全部职位", "0"));
        }

        private void BindDefaultField()
        {
            foreach (ListItem li in Fields)
            {
                lbx_SelectedFields.Items.Add(li);
            }
        }

        private void lbtn_Others_Click(object sender, EventArgs e)
        {
            table_Other.Visible = !table_Other.Visible;
            if (table_Other.Visible)
            {
                lbtn_Others.Text = "其它查询选项 &#171;";
            }
            else
            {
                lbtn_Others.Text = "其它查询选项 &#187;";
            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            Server.Transfer("ResultList.aspx");
        }

        private void btn_In_Click(object sender, EventArgs e)
        {
            if (lbx_Fields.SelectedItem != null)
            {
                lbx_SelectedFields.Items.Add(lbx_Fields.SelectedItem);
                lbx_Fields.Items.Remove(lbx_Fields.SelectedItem);
                if (lbx_Fields.Items.Count != 0)
                {
                    lbx_Fields.Items[0].Selected = true;
                }
                lbx_SelectedFields.Items[lbx_SelectedFields.SelectedIndex].Selected = false;
            }
        }

        private void btn_Out_Click(object sender, EventArgs e)
        {
            if (lbx_SelectedFields.SelectedItem != null)
            {
                lbx_Fields.Items.Add(lbx_SelectedFields.SelectedItem);
                lbx_SelectedFields.Items.Remove(lbx_SelectedFields.SelectedItem);
                if (lbx_SelectedFields.Items.Count != 0)
                {
                    lbx_SelectedFields.Items[0].Selected = true;
                }
                lbx_Fields.Items[lbx_Fields.SelectedIndex].Selected = false;
            }
        }

        private void lbtn_SelectField_Click(object sender, EventArgs e)
        {
            table_Field.Visible = !table_Field.Visible;
            if (table_Field.Visible)
            {
                lbtn_SelectField.Text = "选择显示字段 &#171;";
            }
            else
            {
                lbtn_SelectField.Text = "选择显示字段 &#187;";
            }
        }
    }
}